/*
 * @(#)ColorpickerTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */

package com.dcivision.framework.taglib.html;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.dcivision.framework.web.WebUtil;

/**
  ColorpickerTag.java

  <p>This class is for showing a color panel<p>
  <p>Modification in "struts-html.tld"<br>
  <pre>
     [ADDED]
     &lt;tag&gt;
     &lt;name&gt;colorpicker&lt;/name&gt;
     &lt;tagclass&gt;com.dcivision.framework.taglib.html.ColorpickerTag&lt;/tagclass&gt;
     &ltbodycontent&gtempty&lt/bodycontent&gt
     &lt;attribute&gt;
     &lt;name&gt;property&lt;/name&gt;
     &lt;required&gt;true&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;textOnly&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;popup&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;/tag&gt;
  </pre>
  </p>

  <p>USAGE:</p>
  <p>The following example shows how to show the color panel on a small popup window</p>
  <p><code>&lt;html:colorpicker property='firstName' popup='true'/&gt;</code></p>
  <p>The following example gives an input of hidden type.</p>
  <p><code>&lt;html:colorpicker property='firstName' textOnly='true'/&gt;</code></p>

    @author          Wong Yam Lee
    @company         DCIVision Limited
    @creation date   10/07/2003
    @version         $Revision: 1.10.32.2 $
*/

public class ColorpickerTag extends com.dcivision.framework.taglib.html.TextTag {

  public static final String REVISION = "$Revision: 1.10.32.2 $";

  protected boolean bPopup = false;

  public ColorpickerTag() {
    super();
    setMaxlength("7");
    setSize("7");
  }

  /**
   * Whether pop up a new window or not to show the color panel
   */
  public boolean getPopup() {
    return bPopup;
  }

  public void setPopup(boolean bPopup) {
    this.bPopup = bPopup;
  }

  /**
  * doEndTag
  *
  * Add a hyperlink for showing a color panel if textOnly is false
  *
  * @return  int
  */
  public int doEndTag() throws JspException {
    FormTag formTag = (FormTag) BodyTagSupport.findAncestorWithClass(this, FormTag.class);
    if (!textOnly && !formTag.getTextOnly()) {
      try {
        // print the JavaScript code for showing the color panel
        JspWriter out = pageContext.getOut();
        out.println("<SCRIPT LANGUAGE='JavaScript'>");
        out.println("<!--");
        if ( bPopup ) {
          out.println("var v_" + getProperty() + " = new ColorPicker('window'); // Popup window");
        } else {
          out.println("var v_" + getProperty() + " = new ColorPicker();         // DIV style");
          out.println("v_" + getProperty() + ".writeDiv();");
        }
        out.println("//-->");
        out.println("</SCRIPT>");

        // show the hyperlink
        out.print("<A HREF='#' onClick=\"v_" + getProperty() + ".select(document." + formTag.getBeanName() + "." + getProperty() + ",'pick" + getProperty() + "');return false;\" NAME='pick" + getProperty() + "' ID='pick" + getProperty() + "'>");
        out.print("<img ");
        out.print("src=\""+WebUtil.getImage(pageContext.getRequest(), "IconColorpicker.gif")+"\" ");
        out.print("name=\"imgCalendar_"+property+"\" ");
        out.print("align=\"absmiddle\" ");
        out.print("width=\"34\" height=\"21\" border=\"0\" ");
        out.print("alt=\"\"");
        out.print(">");
        out.println("</A>");
      } catch (Exception e) {
        throw new JspException(e);
      }
    }
    return EVAL_PAGE;
  }

  /**
   * release
   *
   * Release any acquired resources.
   */
  public void release() {
    super.release();
    bPopup = false;
  }

}